//
//  ViewControllerDrawBoard.m
//  核心动画
//
//  Created by chichaojituan521 on 2017/5/31.
//  Copyright © 2017年 linch. All rights reserved.
//

#import "ViewControllerDrawBoard.h"
#import "ViewDrawBoard.h"
#import "DrawLine.h"
@interface ViewControllerDrawBoard ()
{
    NSMutableArray *_arrayHistory;
    UIImageView *_imageView;
}
@end

@implementation ViewControllerDrawBoard
-(void)loadView
{
    ViewDrawBoard *view = [[ViewDrawBoard alloc]init];
    view.backgroundColor = [UIColor lightGrayColor];
    self.view = view;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _arrayHistory=[[NSMutableArray alloc]init];
    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
    
    [self.view addGestureRecognizer:panGesture];
    
    //创建按钮
    UIButton *btnC=[UIButton buttonWithType:UIButtonTypeCustom];
    btnC.frame=CGRectMake(10, 20, 30, 20);
    [btnC setTitle:@"C" forState:UIControlStateNormal];
    btnC.backgroundColor=[UIColor lightGrayColor];
    [btnC addTarget:self action:@selector(cheXiaoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnC];
    
    UIButton *btnR=[UIButton buttonWithType:UIButtonTypeCustom];
    btnR.frame=CGRectMake(10, 50, 30, 20);
    [btnR setTitle:@"R" forState:UIControlStateNormal];
    btnR.backgroundColor=[UIColor lightGrayColor];
    [btnR addTarget:self action:@selector(huiFuAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnR];
}

-(void)panGestureAction:(UIPanGestureRecognizer *)gesture
{
    ViewDrawBoard *viewDraw=(ViewDrawBoard *)gesture.view;
    
    switch (gesture.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            //开始画,创建一个线的对象
            DrawLine *line=[[DrawLine alloc]init];
            line.lineColor=[UIColor redColor];
            line.lineWidth=2;
            [viewDraw.arrayLines addObject:line];
            
            break;
        }
        case UIGestureRecognizerStateChanged:
        {
            CGPoint point = [gesture locationInView:gesture.view];
            DrawLine *lineNew=[viewDraw.arrayLines lastObject];
            
            //将点都加到线上
            [lineNew.points addObject:[NSValue valueWithCGPoint:point]];
            //刷新视图
            [viewDraw setNeedsDisplay];
            //把线加到数组中
            [_arrayHistory addObject:lineNew];
            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            break;
        }
        default:
            break;
    }
}

//撤销
-(void)cheXiaoAction
{
    NSLog(@"---");
    ViewDrawBoard *viewD=(ViewDrawBoard *)self.view;
    DrawLine *line=viewD.arrayLines.lastObject;
    if (line) {
        [_arrayHistory addObject:line];
        [viewD.arrayLines removeObject:line];
        //重绘
        [viewD setNeedsDisplay];
    }
}
//恢复
-(void)huiFuAction
{
    NSLog(@"+++");
    ViewDrawBoard *viewD=(ViewDrawBoard *)self.view;
    DrawLine *line=[_arrayHistory lastObject];
    if (line) {
        [viewD.arrayLines addObject:line];
        [_arrayHistory removeObject:line];
        [viewD setNeedsDisplay];
    }
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

@end
